<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>crypt_test.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="local_closure_goog_crypt_crypt_test.js.html">crypt_test.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2008 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>goog.provide(&#39;goog.cryptTest&#39;);
<a name="line16"></a>goog.setTestOnly(&#39;goog.cryptTest&#39;);
<a name="line17"></a>
<a name="line18"></a>goog.require(&#39;goog.crypt&#39;);
<a name="line19"></a>goog.require(&#39;goog.testing.jsunit&#39;);
<a name="line20"></a>
<a name="line21"></a>var UTF8_RANGES_BYTE_ARRAY = [
<a name="line22"></a>  0x00,
<a name="line23"></a>  0x7F,
<a name="line24"></a>  0xC2, 0x80,
<a name="line25"></a>  0xDF, 0xBF,
<a name="line26"></a>  0xE0, 0xA0, 0x80,
<a name="line27"></a>  0xEF, 0xBF, 0xBF];
<a name="line28"></a>
<a name="line29"></a>var UTF8_RANGES_STRING = &#39;\u0000\u007F\u0080\u07FF\u0800\uFFFF&#39;;
<a name="line30"></a>
<a name="line31"></a>function testStringToUtf8ByteArray() {
<a name="line32"></a>  // Known encodings taken from Java&#39;s String.getBytes(&quot;UTF8&quot;)
<a name="line33"></a>
<a name="line34"></a>  assertArrayEquals(&#39;ASCII&#39;,
<a name="line35"></a>      [72, 101, 108, 108, 111, 44, 32, 119, 111, 114, 108, 100],
<a name="line36"></a>      goog.crypt.stringToUtf8ByteArray(&#39;Hello, world&#39;));
<a name="line37"></a>
<a name="line38"></a>  assertArrayEquals(&#39;Latin&#39;,
<a name="line39"></a>      [83, 99, 104, 195, 182, 110],
<a name="line40"></a>      goog.crypt.stringToUtf8ByteArray(&#39;Sch\u00f6n&#39;));
<a name="line41"></a>
<a name="line42"></a>  assertArrayEquals(&#39;limits of the first 3 UTF-8 character ranges&#39;,
<a name="line43"></a>      UTF8_RANGES_BYTE_ARRAY,
<a name="line44"></a>      goog.crypt.stringToUtf8ByteArray(UTF8_RANGES_STRING));
<a name="line45"></a>}
<a name="line46"></a>
<a name="line47"></a>function testUtf8ByteArrayToString() {
<a name="line48"></a>  // Known encodings taken from Java&#39;s String.getBytes(&quot;UTF8&quot;)
<a name="line49"></a>
<a name="line50"></a>  assertEquals(&#39;ASCII&#39;, &#39;Hello, world&#39;, goog.crypt.utf8ByteArrayToString(
<a name="line51"></a>      [72, 101, 108, 108, 111, 44, 32, 119, 111, 114, 108, 100]));
<a name="line52"></a>
<a name="line53"></a>  assertEquals(&#39;Latin&#39;, &#39;Sch\u00f6n&#39;, goog.crypt.utf8ByteArrayToString(
<a name="line54"></a>      [83, 99, 104, 195, 182, 110]));
<a name="line55"></a>
<a name="line56"></a>  assertEquals(&#39;limits of the first 3 UTF-8 character ranges&#39;,
<a name="line57"></a>      UTF8_RANGES_STRING,
<a name="line58"></a>      goog.crypt.utf8ByteArrayToString(UTF8_RANGES_BYTE_ARRAY));
<a name="line59"></a>}
<a name="line60"></a>
<a name="line61"></a>
<a name="line62"></a>/**
<a name="line63"></a> * Same as testUtf8ByteArrayToString but with Uint8Array instead of
<a name="line64"></a> * Array.&lt;number&gt;.
<a name="line65"></a> */
<a name="line66"></a>function testUint8ArrayToString() {
<a name="line67"></a>  if (!goog.global.Uint8Array) {
<a name="line68"></a>    // Uint8Array not supported.
<a name="line69"></a>    return;
<a name="line70"></a>  }
<a name="line71"></a>
<a name="line72"></a>  var arr = new Uint8Array(
<a name="line73"></a>      [72, 101, 108, 108, 111, 44, 32, 119, 111, 114, 108, 100]);
<a name="line74"></a>  assertEquals(&#39;ASCII&#39;, &#39;Hello, world&#39;, goog.crypt.utf8ByteArrayToString(arr));
<a name="line75"></a>
<a name="line76"></a>  arr = new Uint8Array([83, 99, 104, 195, 182, 110]);
<a name="line77"></a>  assertEquals(&#39;Latin&#39;, &#39;Sch\u00f6n&#39;, goog.crypt.utf8ByteArrayToString(arr));
<a name="line78"></a>
<a name="line79"></a>  arr = new Uint8Array(UTF8_RANGES_BYTE_ARRAY);
<a name="line80"></a>  assertEquals(&#39;limits of the first 3 UTF-8 character ranges&#39;,
<a name="line81"></a>      UTF8_RANGES_STRING,
<a name="line82"></a>      goog.crypt.utf8ByteArrayToString(arr));
<a name="line83"></a>}
<a name="line84"></a>
<a name="line85"></a>function testByteArrayToString() {
<a name="line86"></a>  assertEquals(&#39;&#39;, goog.crypt.byteArrayToString([]));
<a name="line87"></a>  assertEquals(&#39;abc&#39;, goog.crypt.byteArrayToString([97, 98, 99]));
<a name="line88"></a>}
<a name="line89"></a>
<a name="line90"></a>function testHexToByteArray() {
<a name="line91"></a>  assertElementsEquals(
<a name="line92"></a>      [202, 254, 222, 173],
<a name="line93"></a>      // Java magic number
<a name="line94"></a>      goog.crypt.hexToByteArray(&#39;cafedead&#39;));
<a name="line95"></a>
<a name="line96"></a>  assertElementsEquals(
<a name="line97"></a>      [222, 173, 190, 239],
<a name="line98"></a>      // IBM magic number
<a name="line99"></a>      goog.crypt.hexToByteArray(&#39;DEADBEEF&#39;));
<a name="line100"></a>}
<a name="line101"></a>
<a name="line102"></a>function testByteArrayToHex() {
<a name="line103"></a>  assertEquals(
<a name="line104"></a>      // Java magic number
<a name="line105"></a>      &#39;cafedead&#39;,
<a name="line106"></a>      goog.crypt.byteArrayToHex([202, 254, 222, 173]));
<a name="line107"></a>
<a name="line108"></a>  assertEquals(
<a name="line109"></a>      // IBM magic number
<a name="line110"></a>      &#39;deadbeef&#39;,
<a name="line111"></a>      goog.crypt.byteArrayToHex([222, 173, 190, 239]));
<a name="line112"></a>}
<a name="line113"></a>
<a name="line114"></a>
<a name="line115"></a>/** Same as testByteArrayToHex but with Uint8Array instead of Array.&lt;number&gt;. */
<a name="line116"></a>function testUint8ArrayToHex() {
<a name="line117"></a>  if (!goog.isDef(goog.global.Uint8Array)) {
<a name="line118"></a>    // Uint8Array not supported.
<a name="line119"></a>    return;
<a name="line120"></a>  }
<a name="line121"></a>
<a name="line122"></a>  assertEquals(
<a name="line123"></a>      // Java magic number
<a name="line124"></a>      &#39;cafedead&#39;,
<a name="line125"></a>      goog.crypt.byteArrayToHex(new Uint8Array([202, 254, 222, 173])));
<a name="line126"></a>
<a name="line127"></a>  assertEquals(
<a name="line128"></a>      // IBM magic number
<a name="line129"></a>      &#39;deadbeef&#39;,
<a name="line130"></a>      goog.crypt.byteArrayToHex(new Uint8Array([222, 173, 190, 239])));
<a name="line131"></a>}
<a name="line132"></a>
<a name="line133"></a>function testXorByteArray() {
<a name="line134"></a>  assertElementsEquals(
<a name="line135"></a>      [20, 83, 96, 66],
<a name="line136"></a>      goog.crypt.xorByteArray([202, 254, 222, 173], [222, 173, 190, 239]));
<a name="line137"></a>}
<a name="line138"></a>
<a name="line139"></a>
<a name="line140"></a>/** Same as testXorByteArray but with Uint8Array instead of Array.&lt;number&gt;. */
<a name="line141"></a>function testXorUint8Array() {
<a name="line142"></a>  if (!goog.isDef(goog.global.Uint8Array)) {
<a name="line143"></a>    // Uint8Array not supported.
<a name="line144"></a>    return;
<a name="line145"></a>  }
<a name="line146"></a>
<a name="line147"></a>  assertElementsEquals(
<a name="line148"></a>      [20, 83, 96, 66],
<a name="line149"></a>      goog.crypt.xorByteArray(
<a name="line150"></a>          new Uint8Array([202, 254, 222, 173]),
<a name="line151"></a>          new Uint8Array([222, 173, 190, 239])));
<a name="line152"></a>}
</pre>


</body>
</html>
